package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.ParkingLotLogic;
import cn.topevery.garden.entity.dto.ParkingLotDTO;
import cn.topevery.garden.entity.po.ParkingLot;
import cn.topevery.garden.entity.query.ParkingLotQuery;
import cn.topevery.garden.entity.vo.ParkingLotVO;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.enums.GeometryTypeEnum;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Api(tags = "")
@RestController
@RequestMapping("/parkingLot")
public class ParkingLotController extends BaseController {


    @Autowired
    ParkingLotLogic parkingLotLogic;

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/getList")
    public JsonResult<DataPager<ParkingLot>> listPage(@RequestBody ParkingLotQuery query) {
        return JsonResult.ok(parkingLotLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/save")
    public JsonResult<?> saveOrUpdate(@Valid @RequestBody ParkingLotDTO dto) {
        if (dto.getMapRangesPoints() != null && !dto.getMapRangesPoints().isEmpty()) {
            dto.setGeomBd09(GeometryHelper.getGeometry(GeometryHelper.ListtoWellKnownText(dto.mapRangesPoints, GeometryTypeEnum.PLANE)));
        }
        parkingLotLogic.saveOrUpdate(getUser().id.toString(), dto);
        return JsonResult.ok();
    }

    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过id查询设施")
    public JsonResult<?> getById(Integer id) {
        if (id == null) {
            return JsonResult.error("id不能为空");
        }
        ParkingLot po = parkingLotLogic.getEntity(id);
        ParkingLotVO vo = new ParkingLotVO();
        BeanUtils.copyProperties(po, vo);
        if (vo.getGeomBd09() != null) {
            vo.setMapRangesPoints(GeometryHelper.getGeometryPoint(vo.getGeomBd09()));
            vo.setGeomBd09(null);
        }
        return JsonResult.ok(vo);
    }

    @GetMapping(value = "/delById")
    @ApiOperation(value = "通过id删除")
    public JsonResult<?> delById(Integer id) {
        if (id == null) {
            return JsonResult.error("id不能为空");
        }
        parkingLotLogic.delete(getUser().id.toString(), id);
        return JsonResult.ok("删除成功");
    }


    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        parkingLotLogic.deleteBatchById(getUser().userId,ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(ParkingLotQuery query, HttpServletResponse response) {
        List<ParkingLot> list = parkingLotLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", ParkingLot.class, "", response);
    }

}
